<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item label="仓库">
        <el-select
          style="width:120px;"
          v-model="dataForm.wareId"
          placeholder="全部仓库"
          clearable
        >
          <el-option
            :label="w.name"
            :value="w.id"
            v-for="w in wareList"
            :key="w.id"
            @click.native="getDataList()"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          style="width:120px;"
          v-model="dataForm.status"
          placeholder="全部状态"
          clearable
        >
          <el-option
            label="全部状态"
            :value="null"
            @click.native="getDataList()"
          ></el-option>
          <el-option
            label="新建"
            :value="0"
            @click.native="getDataList()"
          ></el-option>
          <el-option
            label="已分配"
            :value="1"
            @click.native="getDataList()"
          ></el-option>
          <el-option
            label="正在换货中"
            :value="2"
            @click.native="getDataList()"
          ></el-option>
          <el-option
            label="已完成"
            :value="3"
            @click.native="getDataList()"
          ></el-option>
          <el-option
            label="换货失败"
            :value="4"
            @click.native="getDataList()"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键字">
        <el-input
          style="width:120px;"
          v-model="dataForm.key"
          placeholder="关键字"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('ware:exchangedetail:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
        <el-dropdown
          @command="handleBatchCommand"
          :disabled="dataListSelections.length <= 0"
        >
          <el-button type="danger">
            批量操作
            <i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="delete">批量删除</el-dropdown-item>
            <el-dropdown-item command="merge">合并整单</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="70"
      >
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="id"
      >
      </el-table-column>
      <el-table-column
        prop="exchangeId"
        header-align="center"
        align="center"
        label="换货单id"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="spuId"
        header-align="center"
        align="center"
        label="需换干货id"
        width="120"
      >
      </el-table-column> -->
      <el-table-column
        prop="spuName"
        header-align="center"
        align="center"
        label="需换干货名称"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="spuNum"
        header-align="center"
        align="center"
        label="需换干货数量"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="oriSpuTotalPrice"
        header-align="center"
        align="center"
        label="需换干货总金额"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="oriSpuPrice"
        header-align="center"
        align="center"
        label="需换干货单价"
        width="120"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="afterSpuId"
        header-align="center"
        align="center"
        label="置换后干货id"
        width="120"
      >
      </el-table-column> -->
      <el-table-column
        prop="afterSpuName"
        header-align="center"
        align="center"
        label="置换后干货名称"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="afterSpuNum"
        header-align="center"
        align="center"
        label="置换后干货数量"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="afterSpuTotalPrice"
        header-align="center"
        align="center"
        label="置换后干货总金额"
        width="140"
      >
      </el-table-column>
      <el-table-column
        prop="afterSpuPrice"
        header-align="center"
        align="center"
        label="置换后干货单价"
        width="120"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="supplierId"
        header-align="center"
        align="center"
        label="供应商id"
        width="120"
      >
      </el-table-column> -->
      <el-table-column
        prop="supplierName"
        header-align="center"
        align="center"
        label="供应商名称"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态"
        width="120"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0">新建</el-tag>
          <el-tag type="info" v-if="scope.row.status == 1">已分配</el-tag>
          <el-tag type="wanring" v-if="scope.row.status == 2"
            >正在换货中</el-tag
          >
          <el-tag type="success" v-if="scope.row.status == 3">已完成</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 4">换货失败</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="wareId"
        header-align="center"
        align="center"
        label="仓库id"
        width="120"
      >
      </el-table-column> -->
      <el-table-column
        prop="wareName"
        header-align="center"
        align="center"
        label="仓库名称"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="oriBatch"
        header-align="center"
        align="center"
        label="需换干货批次"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="afterBatch"
        header-align="center"
        align="center"
        label="置换后干货批次"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="更新时间"
        width="100"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
    <el-dialog title="合并到整单" :visible.sync="mergedialogVisible">
      <!-- id  assignee_id  assignee_name  phone   priority status -->
      <el-select v-model="exchangeId" placeholder="请选择" clearable filterable>
        <el-option
          v-for="item in purchasetableData"
          :key="item.id"
          :label="item.id"
          :value="item.id"
        >
          <span style="float: left">{{ item.id }}</span>
          <span style="float: right; color: #8492a6; font-size: 13px"
            >{{ item.assigneeName }}：{{ item.phone }}</span
          >
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="mergedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="mergeItem">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from "./exchangedetail-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        key: "",
        wareId: "",
        status: ""
      },
      wareList: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      mergedialogVisible: false,
      purchasetableData: [],
      exchangeId: ""
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
    this.getWares();
  },
  methods: {
    //选择删除或是合并的方法
    handleBatchCommand(cmd) {
      if (cmd == "delete") {
        this.deleteHandle();
      }
      if (cmd == "merge") {
        if (this.dataListSelections.length != 0) {
          this.getUnreceivedPurchase();
          this.mergedialogVisible = true;
        } else {
          this.$alert("请先选择需要合并的需求", "提示", {
            confirmButtonText: "确定",
            callback: action => {}
          });
        }
      }
    },
    //获取新建或已分配状态的换货单方法
    getUnreceivedPurchase() {
      this.$http({
        url: this.$http.adornUrl("/ware/exchange/unreceive/list"),
        method: "get",
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        this.purchasetableData = data.page.list;
      });
    },
    //合并进换货单方法
    mergeItem() {
      let items = this.dataListSelections.map(item => {
        return item.id;
      });
      if (!this.exchangeId) {
        this.$confirm(
          "没有选择任何【换货单】，将自动创建新单进行合并。确认吗？",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        )
          .then(() => {
            this.$http({
              url: this.$http.adornUrl("/ware/exchange/merge"),
              method: "post",
              data: this.$http.adornData({ items: items }, false)
            }).then(({ data }) => {
              this.getDataList();
            });
          })
          .catch(() => {});
      } else {
        this.$http({
          url: this.$http.adornUrl("/ware/exchange/merge"),
          method: "post",
          data: this.$http.adornData(
            { exchangeId: this.exchangeId, items: items },
            false
          )
        }).then(({ data }) => {
          this.getDataList();
        });
      }
      this.mergedialogVisible = false;
    },
    getWares() {
      this.$http({
        url: this.$http.adornUrl("/ware/wareinfo/list"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 500
        })
      }).then(({ data }) => {
        this.wareList = data.page.list;
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      console.log(
        this.dataForm.key,
        this.dataForm.status,
        this.dataForm.wareId
      );
      this.$http({
        url: this.$http.adornUrl("/ware/exchangedetail/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
          status: this.dataForm.status,
          wareId: this.dataForm.wareId
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/ware/exchangedetail/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
